package com.bank.user.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.bank.model.entity.*;
import com.bank.model.enums.RoleEnum;
import com.bank.model.vo.user.*;
import com.bank.user.service.ListService;
import com.bank.user.service.UserService;
import com.bank.user.util.ClientFactory;
import com.bank.user.util.EntityFactory;
import com.bank.user.util.MapperFactory;
import com.bank.user.util.VoFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Cejay1314
 * @version 1.0
 * @create 2023/3/30 14:03
 */
@Service
public class ListServiceImpl implements ListService {
    @Autowired
    MapperFactory mapperFactory;
    @Autowired
    UserService userService;
    @Autowired
    EntityFactory entityFactory;
    @Autowired
    VoFactory voFactory;
    @Autowired
    ClientFactory clientFactory;


    @Override
    public List<EmployeeVo> getEmployeeVo() {
        List<User> users = userService.getUsers(RoleEnum.Counter.getMessage());
        List<EmployeeVo> employeeVos = new ArrayList<>();
        for(User user:users){
            employeeVos.add(voFactory.getEmployeeVo(user,RoleEnum.Counter.getMessage()));
        }
        users = userService.getUsers(RoleEnum.Manager.getMessage());
        for(User user:users){
            employeeVos.add(voFactory.getEmployeeVo(user,RoleEnum.Manager.getMessage()));
        }
        return employeeVos;
    }

    @Override
    public List<UserVo> getUserVo() {
        List<User> users = userService.getUsers(RoleEnum.User.getMessage());
        List<UserVo> userVos = new ArrayList<>();
        for(User user:users){
            userVos.add(voFactory.getUserVo(user));
        }
        return userVos;
    }

    @Override
    public List<DepositListVo> getDepositListVo() {
        List<User> users = userService.getUsers(RoleEnum.User.getMessage());
        List<DepositListVo> depositListVos = new ArrayList<>();
        for(User user:users){
            Double money = clientFactory.getDepositService().getUserMoney(user.getId());
            depositListVos.add(voFactory.getDepositListVo(user,money));
        }
        return depositListVos;
    }

    @Override
    public List<LoanListVo> getLoanListVo(){
        List<User> users = userService.getUsers(RoleEnum.User.getMessage());
        List<LoanListVo> loanListVos = new ArrayList<>();
        for(User user:users){
            List<Loan> loans = clientFactory.getLoanService().queryUser(user.getId());
            Double totalMoney = new Double(0);
            Double paidMoney = new Double(0);
            for(Loan loan:loans){
                totalMoney+= Double.parseDouble(loan.getTotalMoney());
                paidMoney+= Double.parseDouble(loan.getPaidMoney());
            }
            loanListVos.add(voFactory.getLoanListVo(user,totalMoney,paidMoney));
        }
        return loanListVos;
    }

    @Override
    public List<PositionDistributionVo> getPositionDistribution() {
        List<PositionDistributionVo> positionDistributionVo = new ArrayList<>();
        for(Map.Entry<String, Role> item: entityFactory.getRolesMap().entrySet()){
            positionDistributionVo.add(voFactory.getPositionDistributionVo(item.getKey(),getUserQuantity(item.getValue().getId())));
        }
        return positionDistributionVo;
    }
    @Override
    public Integer getUserQuantity(Long roleId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        int x = mapperFactory.getUserRoleMapper().selectCount(queryWrapper);
        return x;
    }

    @Override
    public List<UserBasicInfoVo> getUserBasicInfoVo(String roleName) {
        List<User> users = userService.getUsers(roleName);
        List<UserBasicInfoVo> userBasicInfoVos = voFactory.getUserBasicInfoVos(users);
        return userBasicInfoVos;
    }
}
